package com.etc;

import com.sun.org.apache.regexp.internal.RE;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

/**
 * @Auther: zhezhe
 * @Date: 2018/10/2614:20
 * @Description:
 */
public class FlowSubmitJob {

    /**
     * map处理的是一行的数据
     * LongWritable是偏移量,Text是读取的一行数据
     */
    public static class FlowMapper extends Mapper<LongWritable, Text,Text,FlowBan>{
        /**
         * map承担主要的处理工作，把输入数据拆分为键值对。
         * map()方法的输入是一个键和一个值，输出是一个Context实例：
         * @param key
         * @param value
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //将text格式的字段转为java的String类型
            String line = value.toString();
            String[] s = line.split("\t");
            //指定电话号码，上行流量下行流量在数组中的位置
            String phone = s[1];
            int upFlow = Integer.valueOf(s[s.length-3]) ;//Integer.valueOf和Integer.parseInt都可将String型转为int型
            int dFlow = Integer.parseInt(s[s.length-2]);
           //将电话号码作为key，FlowBan对象作为value发出
            context.write(new Text(phone),new FlowBan(phone,upFlow,dFlow));
            
        }
    }


    public static class FlowReduce extends Reducer<Text,FlowBan,FlowBan, NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<FlowBan> values, Context context) throws IOException, InterruptedException {
            //upFlow xiaFlow 初始化为0
            int upFlow = 0;
            int xiaFlow = 0;
            //遍历values，每个value获取upFlow  xiaFlow值
            for (FlowBan value : values) {
               upFlow = value.getUpFlow();
               xiaFlow = value.getdFlow();
            }
            //FlowBan对象为key，value为空发出
            context.write(new FlowBan(key.toString(),upFlow,xiaFlow),NullWritable.get());
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        System.setProperty("HADOOP_USER_NAME","root");
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(FlowSubmitJob.class);

        job.setMapperClass(FlowSubmitJob.FlowMapper.class);
        job.setReducerClass(FlowSubmitJob.FlowReduce.class);


        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FlowBan.class);


        job.setOutputKeyClass(FlowBan.class);
        job.setOutputValueClass(NullWritable.class);

        File file = new File("E:\\案例一流量统计求和\\ouj0ut");
        if (file.exists()){
            FileUtil.fullyDelete(file);
        }

        FileInputFormat.addInputPath(job,new Path("E:\\案例一流量统计求和\\练习用的数据"));
        FileOutputFormat.setOutputPath(job,new Path("E:\\案例一流量统计求和\\ouj0ut"));


        job.setNumReduceTasks(1);

        System.exit(job.waitForCompletion(true) ? 0 :1 );

    }

}
